package cn.school.workflow.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.school.common.utils.SysUserUtils;
import cn.school.workflow.dao.DocumentApplyDao;
import cn.school.workflow.service.DocumentApplyService;
import cn.school.workflow.service.WorkFlowCommonService;
import cn.school.workflow.vo.DocumentApplyVO;
import cn.school.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 公文流转申请服务类
 */
@Service
public class DocumentApplyServiceImpl  extends ServiceImpl<DocumentApplyDao, DocumentApplyVO> implements DocumentApplyService {

    @Autowired
    private WorkFlowCommonService workFlowCommonService;

    /**
     * 分页查公文信息
     * @param vo
     * @return
     */
    @Override
    public IPage<DocumentApplyVO> findDocumentApplyPageList(DocumentApplyVO vo) {
        LambdaQueryWrapper<DocumentApplyVO> wrapper = new LambdaQueryWrapper<>(vo);
        if(!StringUtils.isEmpty(vo.getApplyNo())){
            wrapper.eq(DocumentApplyVO::getApplyNo,vo.getApplyNo());
        }
        wrapper.orderByDesc(DocumentApplyVO::getCreateTime);
        return page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper);
    }

    /**
     * 撤销流程
     * @param vo
     * @return
     */
    @Override
    public boolean cancelDocumentApply(WorkFlowVO vo) {
        LambdaQueryWrapper<DocumentApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocumentApplyVO::getProcessId,vo.getProcessInstanceId());
        DocumentApplyVO applyVO = new DocumentApplyVO();
        applyVO.setApplyStatus("2");//撤销流程
        this.baseMapper.update(applyVO,wrapper);
        return workFlowCommonService.cancelApply(vo.getProcessInstanceId(),vo.getApplyDesc());
    }

    @Override
    public int createDocumentApply(DocumentApplyVO vo) {
        String username = SysUserUtils.getSysUser().getUsername();
        //获取用户下级审批人
//        String assignee = workFlowCommonService.findAssigneeList();
        String applyNo = "DC_"+ DateUtil.format(new Date(),"yyyyMMddHHmmssSSS");
        vo.setApplyNo(applyNo);
        vo.setCreateBy(username);
        vo.setCreateTime(new Date());
        //创建申请单
        int num = this.baseMapper.insert(vo);
        if(StringUtils.isEmpty(vo.getApplyStatus()) && vo.getApplyStatus().equals("0")){//草稿状态不提交流程
            return num;
        }
        WorkFlowVO flowVO = new WorkFlowVO();
        flowVO.setApplyTitle("公文流转申请");
        flowVO.setApplyID("documentApply");
        flowVO.setBusinessKey(applyNo);
        //创建请假工作流
        String processInstanceId = workFlowCommonService.createProcessInstance(flowVO);
        vo.setProcessId(processInstanceId);
        //更新流程实例ID
        return this.baseMapper.updateById(vo);
    }

    @Override
    public DocumentApplyVO findDocumentApplyDetail(Long processInstanceId) {
        LambdaQueryWrapper<DocumentApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocumentApplyVO::getProcessId,processInstanceId);
        return this.baseMapper.selectOne(wrapper);
    }
}
